package api

import (
	"Suscan/pkg/e"
	"Suscan/pkg/scan"
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"time"
)

//扫描开关

var Scantest bool

func StartScan(c *gin.Context) {
	Scantest = true
	go func() {
		defer func() {
			if err := recover(); err != nil {
				fmt.Println("masscan 运行错误:", err)
			}
		}()

		i := 1
		for {
			if !Scantest {
				break
			}
			start := time.Now()
			scan.InitMasscan()
			end := time.Now()
			log.Printf("masscan完成第 %d 轮扫描 耗时: %s\n", i,end.Sub(start))
			i++
			time.Sleep(10 * time.Second)
		}
	}()
	code := e.SUCCESS
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": Scantest,
	})
}

func StoptScan(c *gin.Context) {
	Scantest = false
	code := e.SUCCESS

	fmt.Println("关闭扫描")
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": Scantest,
	})
}

func ScanStatus(c *gin.Context) {
	code := e.SUCCESS

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": Scantest,
	})
}